package org.shop.validate.context;
// +----------------------------------------------------------------------

// | OnlineRetailers [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2003-2023 www.yisu.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 亿速网络（http://www.yisu.cn）
// +----------------------------------------------------------------------
// | Author: 王强 <13052079525>
// +----------------------------------------------------------------------
// |简单与丰富！
// +----------------------------------------------------------------------
// |让外表简单一点，内涵就会更丰富一点。
// +----------------------------------------------------------------------
// |让需求简单一点，心灵就会更丰富一点。
// +----------------------------------------------------------------------
// |让言语简单一点，沟通就会更丰富一点。
// +----------------------------------------------------------------------
// |让私心简单一点，友情就会更丰富一点。
// +----------------------------------------------------------------------
// |让情绪简单一点，人生就会更丰富一点。
// +----------------------------------------------------------------------
// |让环境简单一点，空间就会更丰富一点。
// +----------------------------------------------------------------------
// |让爱情简单一点，幸福就会更丰富一点。
// +----------------------------------------------------------------------

import java.util.HashMap;
import java.util.Map;
import org.shop.common.until.FirstToUpper;


/**
 * 检测参数
 * 
 * @author 王强
 */
public class CheckParam implements Cloneable {
	/**
	 * 消息提示
	 * 
	 * @var array
	 */
	private Map<String, Map<String, String>> messageNotice;

	// 数据验证路径
	private final static String VALIDATE_MASTER_PATH = "org.shop.validate.impl.";

	/**
	 * 检测类对象实例
	 * 
	 * @var array
	 */
	private static Map<String, Object> obj = new HashMap<String, Object>();

	/**
	 * 反射类对象实例
	 * 
	 * @var array
	 */
	private static Map<String, Class<?>> reflectionClass = new HashMap<String, Class<?>>();

	/**
	 * 错误消息
	 * 
	 * @var string
	 */
	private String errorMessage = "";

	/**
	 * 待验证参数
	 * 
	 * @var array
	 */
	private Map<String, String> data;

	/**
	 * @return the errorMessage
	 */
	public String getErrorMessage() {
		return this.errorMessage;
	}

	public CheckParam(Map<String, Map<String, String>> messageNotice, Map<String, String> data) {
		this.messageNotice = messageNotice;

		this.data = data;
	}

	/**
	 * @return the messageNotice
	 */
	public Map<String, Map<String, String>> getMessageNotice() {
		return this.messageNotice;
	}

	/**
	 * @param multitype:
	 *            messageNotice
	 */
	public void setMessageNotice(Map<String, Map<String, String>> messageNotice) {
		this.messageNotice = messageNotice;
	}

	/**
	 * 检测参数
	 * 
	 * @param array
	 *            validateKey 验证信息
	 * @param string
	 *            key 待验证数据的key
	 * @return bool
	 */
	protected boolean paramCheckNotify(Map<String, String> validateKey, String key) {
		String className = "";

		Class<?> reflectionObj;
		//
		Object validateObj = null;
		//
		boolean result = false;
		//
		Object object = null;
		//
		Class<?> ref = null;
		//
		try {
			for (Map.Entry<String, String> entity : validateKey.entrySet()) {
				className = VALIDATE_MASTER_PATH + FirstToUpper.getInstance(entity.getKey()).upperCase();

				if (obj.get(className) == null) {

					reflectionObj = Class.forName(className);

					validateObj = reflectionObj.getConstructor(new Class<?>[] { String.class, String.class })
							.newInstance(this.data.get(key), entity.getValue());

					result = (Boolean) reflectionObj.getMethod("check").invoke(validateObj);
					obj.put(className, validateObj);
					//
					reflectionClass.put(className, reflectionObj);

				} else {
					object = obj.get(className);

					ref = reflectionClass.get(className);

					ref.getMethod("setData", String.class).invoke(object, new Object[] { this.data.get(key) });

					ref.getMethod("setMessage", String.class).invoke(object, new Object[] { entity.getValue() });

					result = (Boolean) ref.getMethod("check").invoke(object);
				}

				if (result == false) {
					this.errorMessage = entity.getValue();
					return false;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 检测参数
	 * 
	 * @return boolean
	 */
	public boolean checkParam() {
		Map<String, Map<String, String>> message = this.messageNotice;

		if (message.isEmpty() || this.data.isEmpty()) {
			return false;
		}

		boolean result = false;

		for (Map.Entry<String, Map<String, String>> entry : message.entrySet()) {

			result = this.paramCheckNotify(entry.getValue(), entry.getKey());

			if (result == false) {
				return false;
			}
		}
		return true;
	}
}